package org.flexthinker.binding.bean
{
    import mx.events.PropertyChangeEvent;

    internal class BindingGroupInternalListener implements BindingListener
    {
        private var group:BindingGroup;

        public function BindingGroupInternalListener(group:BindingGroup):void
        {
            this.group = group;
        }

        private function get listeners():Vector.<BindingListener>
        {
            return group.getBindingListeners();
        }

        public function bindingBecameBound(binding:Binding):void
        {
            var n:int = listeners.length;
            for (var i:int = 0; i < n; i++)
            {
                listeners[i].bindingBecameBound(binding);
            }

            if (group.bound.indexOf(binding) < 0)
            {
                group.bound.push(binding);
            }
            if (group.unbound.indexOf(binding) >= 0)
            {
                group.unbound.splice(group.unbound.indexOf(binding), 1);
            }
        }

        public function bindingBecameUnbound(binding:Binding):void
        {
            var n:int = listeners.length;
            for (var i:int = 0; i < n; i++)
            {
                listeners[i].bindingBecameUnbound(binding);
            }

            if (group.bound.indexOf(binding) >= 0)
            {
                group.bound.splice(group.bound.indexOf(binding), 1);
            }
            if (group.unbound.indexOf(binding) < 0)
            {
                group.unbound.push(binding);
            }
        }

        public function sourceChanged(binding:Binding, event:PropertyChangeEvent):void
        {
            var n:int = listeners.length;
            for (var i:int = 0; i < n; i++)
            {
                listeners[i].sourceChanged(binding, event);
            }
        }

        public function synced(binding:Binding):void
        {
            var n:int = listeners.length;
            for (var i:int = 0; i < n; i++)
            {
                listeners[i].synced(binding);
            }
        }

        public function syncFailed(binding:Binding, failure:SyncFailure):void
        {
            var n:int = listeners.length;
            for (var i:int = 0; i < n; i++)
            {
                listeners[i].syncFailed(binding, failure);
            }
        }

        public function targetChanged(binding:Binding, event:PropertyChangeEvent):void
        {
            var n:int = listeners.length;
            for (var i:int = 0; i < n; i++)
            {
                listeners[i].targetChanged(binding, event);
            }
        }
    }
}
